package com.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.commons.exception.CreateTypeException;
import com.management.commons.exception.DeleteTypeException;
import com.management.commons.exception.ExceptionCode.DefectTypeExceptionCode;
import com.management.commons.exception.DisableByTypeIdException;
import com.management.commons.exception.GetTypeException;
import com.management.controller.form.*;
import com.management.controller.info.GetAllTypeInfo;
import com.management.controller.info.GetTypeInfo;
import com.management.model.DefectType;
import com.management.dao.DefectTypeDao;
import com.management.service.DefectTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.management.utils.CopyUtils;
import com.management.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-03-23
 */
@Service
public class DefectTypeServiceImpl extends ServiceImpl<DefectTypeDao, DefectType> implements DefectTypeService {

    /**
     * 从容器中取出缺陷类型的Dao对象
     */
    @Resource
    private DefectTypeDao defectTypeDao;

    /**
     * 获取所有缺陷类型的业务层接口的实现方法
     * @param getTypePageForm
     * @return
     */
    @Override
    public Result FindType(GetTypePageForm getTypePageForm) {
        Page<DefectType> Page = new Page<>();
        GetTypePageForm typeForm=null;
        if (null== getTypePageForm.getTypeDesc()) {
            typeForm = defectTypeDao.selectPage(getTypePageForm, null);
        }else {
            typeForm = defectTypeDao.selectPage(getTypePageForm, new QueryWrapper<DefectType>().like("type_desc", getTypePageForm.getTypeDesc()));
        }
        if (null!=typeForm) {
            List<DefectType> records = typeForm.getRecords();
            List list = CopyUtils.CopyList(records, GetTypeInfo.class);

            Page.setTotal(typeForm.getTotal());
            Page.setCurrent(typeForm.getCurrent());
            Page.setSize(typeForm.getSize());
            Page.setPages(typeForm.getPages());
            Page.setRecords(list);
            return Result.getInstance().setData(Page);
        }
        else throw new GetTypeException(DefectTypeExceptionCode.GETTYPE_NULL_ERROR);
    }

    /**
     * 新增缺陷类型的业务层接口的实现方法
     * @param createTypeForm
     * @return
     */
    @Override
    public Result addType(CreateTypeForm createTypeForm) {
        QueryWrapper<DefectType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_desc",createTypeForm.getTypeDesc());
        DefectType defectType1 = baseMapper.selectOne(queryWrapper);
        if (defectType1==null) {
            DefectType defectType = CopyUtils.CopyOne(createTypeForm, DefectType.class);
            int i = defectTypeDao.insert(defectType);
            if (i>0) {
                return Result.getInstance().setMessage("新增成功");
            }
            else throw new CreateTypeException(DefectTypeExceptionCode.CREATETYPE_ERROR);
        }
        throw new CreateTypeException(DefectTypeExceptionCode.DEFECTTYPEISEXISTS_ERROR);
    }

    /**
     * 移除前端传过来的typeId的缺陷类型
     * @param removeTypeForm
     * @return
     */
    @Override
    public Result removType(RemoveTypeForm removeTypeForm) {
        int i = defectTypeDao.deleteById(removeTypeForm.getTypeId());
        if (i>0) {
            return Result.getInstance().setMessage("删除成功");
        }
        else throw new DeleteTypeException(DefectTypeExceptionCode.DELETETYPE_ERROR);
    }

    @Override
    public Result removeBytypeIds(RemoveBytypeIdsForm removeBytypeIdsForm) {
        int i=0;
        for (Integer integer : removeBytypeIdsForm.getList()) {
             i += defectTypeDao.deleteById(integer);
        }
        if (i>0) {
            return Result.getInstance().setMessage("删除成功");
        }
        else throw new DeleteTypeException(DefectTypeExceptionCode.DELETETYPE_ERROR);
    }

    @Override
    public Result updateBytypeId(DisableTypeForm disableTypeForm) {
        DefectType defectType = defectTypeDao.selectById(disableTypeForm.getTypeId());
        if (defectType.getModuleState().equals("启用")) {
            defectType.setModuleState("禁用");
        }else {
            defectType.setModuleState("启用");
        }
        int i = defectTypeDao.updateById(defectType);
        if (i>0) {
            return Result.getInstance().setMessage("修改成功");
        }
        else throw new DisableByTypeIdException(DefectTypeExceptionCode.DISABLEBYTYPEID_ERROR);
    }

    @Override
    public Result FindAllType() {
        List<DefectType> defectTypes = defectTypeDao.selectList(null);
        List list = CopyUtils.CopyList(defectTypes, GetAllTypeInfo.class);
        return Result.getInstance().setData(list);
    }


}
